#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/wait.h>

#define MSG "This message is from parent :Hello ,child process"
int main(int argc,char **argv)
{
   int  pipe_fd[2];
   int  rv;
   int  pid;
   char buf[512];
   int  wstatus;
   if(pipe(pipe_fd)<0)
   {
      printf("pipe failure:%s\n",strerror(errno));
      return -2;
   }
   if((pid=fork())<0)
   {
     printf("create chiled failure:%s\n",strerror(errno));
   }
   else if(pid==0)
   {
     close(pipe_fd[1]);
     memset(buf,0,sizeof(buf));
     rv=read(pipe_fd[0],buf,sizeof(buf));
     if(rv<0)
     {
       printf("child read from pipe failure:%s\n",strerror(errno));
       return -3;
     }
     printf("child read %d bytes from pipe:\"%s\"\n",rv,buf);
     return 0;
   }
   close(pipe_fd[0]);
   if(write(pipe_fd[1],MSG,strlen(MSG))<0)
   {
     printf("parent write data to pipe fail:%s\n",strerror(errno));
     return -4;
   }
   printf("Parent start wait child process exit ..\n");
   wait(&wstatus);
   return 0;
}
